package com.example.music.model;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Parcel;
import android.os.Parcelable;

import com.example.music.db.FavoritesDAO;

public class Song implements Parcelable {
    private String title;       // 歌曲名
    private String artist;      // 歌手
    private String album;       // 专辑
    private String path;        // 文件路径
    private long duration;      // 时长（毫秒）
    private transient Bitmap albumCover; // 封面
    private String lyricsPath;  // 歌词文件路径

    public Song(String title, String artist, String album, String path) {
        this.title = title;
        this.artist = artist;
        this.album = album;
        this.path = path;
        this.lyricsPath = null; // 初始化为null
    }

    protected Song(Parcel in) {
        title = in.readString();
        artist = in.readString();
        album = in.readString();
        path = in.readString();
        duration = in.readLong();
        lyricsPath = in.readString();
        // Do not read the Bitmap object directly from the Parcel
    }

    public static final Creator<Song> CREATOR = new Creator<Song>() {
        @Override
        public Song createFromParcel(Parcel in) {
            return new Song(in);
        }

        @Override
        public Song[] newArray(int size) {
            return new Song[size];
        }
    };

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getArtist() {
        return artist;
    }

    public void setArtist(String artist) {
        this.artist = artist;
    }

    public String getAlbum() {
        return album;
    }

    public void setAlbum(String album) {
        this.album = album;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public long getDuration() {
        return duration;
    }

    public void setDuration(long duration) {
        this.duration = duration;
    }

    public Bitmap getAlbumCover() {
        return albumCover;
    }

    public void setAlbumCover(Bitmap albumCover) {
        this.albumCover = albumCover;
    }

    public String getLyricsPath() {
        return lyricsPath;
    }

    public void setLyricsPath(String lyricsPath) {
        this.lyricsPath = lyricsPath;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(title);
        dest.writeString(artist);
        dest.writeString(album);
        dest.writeString(path);
        dest.writeLong(duration);
        dest.writeString(lyricsPath);
        // Do not write the Bitmap object directly to the Parcel
    }
    public boolean isFavorite(Context context) {
        FavoritesDAO dao = new FavoritesDAO(context);
        return dao.isFavorite(this.path);
    }

    public void toggleFavorite(Context context) {
        FavoritesDAO dao = new FavoritesDAO(context);
        if (isFavorite(context)) {
            dao.removeFavorite(this.path);
        } else {
            dao.addFavorite(this.path);
        }
    }
}